"""
问卷
"""
from fastapi import APIRouter, Depends, Response, Query
from pydantic import BaseModel
from typing import Optional, List
from sqlalchemy.orm import Session

from schemas import *
from app.dependencies import get_db
from utils import await_task, resp_detail_200
from sql_app.crud import crud_questionnaire, crud_question
from utils.response import resp_200


router = APIRouter(
    prefix="/questionnaire",
    tags=["questionnaire"],
    responses={404: {"description": "Not Found"}}
)


@router.post("/add_category")
async def add_category(
        questionnaire: QuestionnaireCreate, db: Session=Depends(get_db)):
    await await_task(crud_questionnaire.create, db, questionnaire)
    return resp_200()


@router.get("/get_category_list")
async def get_category_list(
        accesstoken: str = Query(..., example="TOKEN3590"),
        pageidx: int = Query(..., example = 1),
        pagesize: int = Query(..., example = 20), keyword: str = None,
        db: Session = Depends(get_db)) -> Response:
    total = await await_task(crud_questionnaire.get_count, db)
    data = await await_task(
        crud_questionnaire.get_a_batch, db, (pageidx-1)*pagesize, pagesize, keyword
    )
    print(Questionnaire.from_orm(data[0]))
    return resp_200(total=total, data=[Questionnaire.from_orm(i) for i in data])


@router.post("/delete_category")
async def delete_category(
        activity: QuestionnaireDelete, db: Session = Depends(get_db)):
    await await_task(crud_questionnaire.delete, db, activity)
    return resp_200()


@router.get("/get_category_info")
async def get_category_info(
        accesstoken: str=Query(..., example="TOKEN3590"),
        id: int=Query(..., example=1),
        db: Session = Depends(get_db)
):
    data = await await_task(
        crud_questionnaire.get_detail, db, id)

    return resp_detail_200(data=Questionnaire.from_orm(data))


@router.post("/adjust_category")
async def adjust_category(
        questionnaire: QuestionnaireAdjust, db: Session = Depends(get_db)):
    await await_task(crud_questionnaire.adjust, db, questionnaire)
    return resp_200()


@router.get("/get_question_list")
async def get_question_list(
        accesstoken: str = Query(..., example="TOKEN3590"),
        pageidx: int = Query(..., example = 1),
        pagesize: int = Query(..., example = 20), keyword: str = None,
        db: Session = Depends(get_db)) -> Response:
    total = await await_task(crud_question.get_count, db)
    data = await await_task(
        crud_question.get_a_batch, db, (pageidx-1)*pagesize, pagesize, keyword
    )

    return resp_200(total=total, data=[Question.from_orm(i) for i in data])


@router.post("/add_question")
async def add_question(
        question: QuestionCreate, db: Session=Depends(get_db)):
    await await_task(crud_question.create, db, question)
    return resp_200()


@router.post("/adjust_question")
async def adjust_question(question: QuestionAdjust, db: Session = Depends(get_db)):
    await await_task(crud_question.adjust, db, question)
    return resp_200()


@router.post("/delete_question")
async def delete_question(question: QuestionDelete, db: Session = Depends(get_db)):
    await await_task(crud_question.delete, db, question)
    return resp_200()


@router.get("/get_question_info")
async def get_question_info(
        accesstoken: str = Query(..., example="TOKEN3590"),
        id: int = Query(..., example = 1),
        db: Session = Depends(get_db)
):
    data = await await_task(
        crud_question.get_detail, db, id)

    return resp_detail_200(data=Question.from_orm(data))


